package com.everwin.web.controller.system;

import com.everwin.common.annotation.Log;
import com.everwin.common.core.controller.BaseController;
import com.everwin.common.core.domain.AjaxResult;
import com.everwin.common.core.page.TableDataInfo;
import com.everwin.common.enums.BusinessType;
import com.everwin.common.utils.poi.ExcelUtil;
import com.everwin.framework.util.ShiroUtils;
import com.everwin.system.domain.SysDictData;
import com.everwin.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典信息

 */
@Api("数据字典详细信息")
@Controller
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @ApiOperation("查询数据字典信息")
    @ApiImplicitParam(name = "dictTypes", value = "字典类型", dataType = "String", paramType = "query", required = true)
    @PostMapping("/all")
    @ResponseBody
    public AjaxResult all(String dictTypes)
    {
        JSONObject json = null;
        if(dictTypes.startsWith("{")) json = new JSONObject(dictTypes);
        JSONArray array = json.getJSONArray("keys");
        Map<String,List<Map<String,String>>> map = new HashMap<>();
        for (Object dictType : array) {
            List<SysDictData> lists = dictDataService.selectDictDataByType((String) dictType);
            List<Map<String,String>> arr = new ArrayList<>();
            for(SysDictData data : lists) {
                Map<String,String> m = new HashMap<>();
                m.put("text",data.getDictLabel());
                m.put("value",data.getDictValue());
                arr.add(m);
            }
            map.put((String) dictType,arr);
        }
        return AjaxResult.success("success",map);
    }

    @ApiOperation("查询数据字典分页")
    @ApiImplicitParam(name = "sysDictData", value = "查询字典信息", dataType = "SysDictData")
    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public TableDataInfo list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @ApiOperation("导出数据字典")
    @ApiImplicitParam(name = "sysDictData", value = "查询字典信息", dataType = "SysDictData")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        return util.exportExcel(list, "字典数据");
    }

    /**
     * 新增保存字典类型
     */
    @ApiOperation("新增保存字典")
    @ApiImplicitParam(name = "sysDictData", value = "字典信息",required = true,dataType = "SysDictData")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDictData dict)
    {
        dict.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation("修改保存字典")
    @ApiImplicitParam(name = "sysDictData", value = "字典信息" ,required = true, dataType = "SysDictData")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDictData dict)
    {
        dict.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(dictDataService.updateDictData(dict));
    }

    @ApiOperation("删除字典")
    @ApiImplicitParam(name = "ids", value = "字典ID", required = true, dataType = "String", paramType = "path")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dictDataService.deleteDictDataByIds(ids));
    }
}
